<script setup>
import PageContainer from '@/components/pageContainer.vue'
import {
  addCategoryService,
  deleteCategoryService,
  editCategoryService,
  getCategoryListService
} from '@/api/category/category'
import { onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const page = ref(1)
const pageSize = ref(9)
const total = ref(0)
const categoryList = ref()
const queryParams = ref({
  page: 1,
  pageSize: 9,
  name: null
})
const getCategoryList = async () => {
  // queryParams.value.name='其他'
  const res = await getCategoryListService(queryParams.value)
  total.value = res.data.data.total
  categoryList.value = res.data.data.records
}
const dialogVisible = ref(false)
const title = ref('')
const formModel = ref({})
const rules = {
  name: [{ required: true, message: '分类名称不能为空', trigger: 'change' }],
  type: [{ required: true, message: '分类类别不能为空', trigger: 'change' }]
}
const formRef = ref(null)
const handleBeforeClose = () => {
  if (formRef.value) {
    formRef.value.clearValidate()
  }
  formModel.value = {}
  dialogVisible.value = false
  getCategoryList()
}

const addCategoryHandle = () => {
  if (formRef.value) {
    formRef.value.clearValidate()
  }
  title.value = '添加分类'
  dialogVisible.value = true
}
const handleSubmit = async () => {
  if (!formModel.value.id) {
    const data = {
      name: formModel.value.name,
      type: formModel.value.type
    }
    const res = await addCategoryService(data)
    if (res.data.code === 1) {
      ElMessage.success('添加成功')
    } else {
      ElMessage.error(res.data.data.msg)
    }
  } else {
    const res = await editCategoryService(formModel.value)
    if (res.data.code === 1) {
      ElMessage.success('修改成功')
    } else {
      ElMessage.error(res.data.msg)
    }
  }
  handleBeforeClose()
}
const editCategory = (row) => {
  title.value = '修改分类'
  formModel.value = row
  dialogVisible.value = true
}
const deleteCategory = (row) => {
  ElMessageBox.confirm('确认要删除该分类吗', '提示', {
    customClass: 'el-message-box-myself',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    confirmButtonClass: 'ElMessageBox-confirm-button-style',
    cancelButtonClass: 'ElMessageBox-cancel-button-style'
  })
    .then(async () => {
      console.log(row.id)
      const res = await deleteCategoryService(row.id)
      if (res.data.code === 1) {
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
        await getCategoryList()
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '删除取消'
      })
    })
}
const resetForm = () => {
  queryParams.value.page = 1
  queryParams.value.name = null
  getCategoryList()
}
onMounted(() => {
  getCategoryList()
})
</script>

<template>
  <el-dialog
    style="border-radius: 25px"
    v-model="dialogVisible"
    width="35%"
    :title="title"
    :modal="true"
    :before-close="handleBeforeClose"
  >
    <el-form :model="formModel" :rules="rules" ref="formRef">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="分类名称" prop="name">
            <el-input v-model="formModel.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分类类型" prop="type">
            <el-input v-model="formModel.type"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <div style="position: absolute; right: 0; margin-top: 30px">
          <el-button
            color="#8fbc8f"
            type="primary"
            plain
            round
            @click="handleSubmit"
            >确定
          </el-button>
          <el-button type="info" plain round @click="handleBeforeClose"
            >取消
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </el-dialog>

  <div class="floating-box">
    <div>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        label-width="68px"
        class="no-wrap"
        @keydown.enter.prevent
      >
        <!--        <el-form-item label="类别" prop="name">-->
        <!--          <el-input-->
        <!--              prefix-icon="Search"-->
        <!--              v-model="queryParams.name"-->
        <!--              placeholder="请输入分类名称"-->
        <!--              clearable-->
        <!--              style="width: 200px;"-->
        <!--              @keyup.enter="getCategoryList()"-->
        <!--          />-->
        <!--        </el-form-item>-->
        <el-form-item>
          <!--          <el-button icon="Search" round @click="getCategoryList()">搜索</el-button>-->
          <el-button
            icon="Refresh"
            plain
            round
            color="#95d475"
            @click="resetForm()"
            >重置</el-button
          >
          <el-button
            icon="CirclePlus"
            plain
            round
            color="#95d475"
            @click="addCategoryHandle()"
            >新增分类
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-divider />
    <el-table :data="categoryList" width="100%" :stripe="true" size="large">
      <el-table-column
        prop="name"
        label="分类名称"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="type"
        label="分类类型"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        label="最后操作时间"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template #default="{ row }">
          <el-button
            type="primary"
            color="#8fbc8f"
            plain
            round
            icon="Edit"
            @click="editCategory(row)"
            >编辑
          </el-button>
          <el-button
            type="info"
            plain
            round
            icon="Delete"
            @click="deleteCategory(row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        layout="total, prev, pager, next, jumper"
        v-show="total > 0"
        :total="total"
        @size-change="getCategoryList"
        @current-change="getCategoryList"
      />
    </div>
  </div>
</template>

<style>
.el-message-box-myself {
  border-radius: 25px;
}

.ElMessageBox-confirm-button-style {
  background: darkseagreen !important;
  color: white !important;
}

.ElMessageBox-cancel-button-style {
  background: white;
  color: black !important;
}
</style>
